package com.example.admin.wechat;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageButton mbtnWeChat;
    private ImageButton mbtnContact;
    private  ImageButton mbtnFind;
    private  ImageButton mbtnMe;

    private WeChatFragment weChatFragment = null;
    private ContactFragment contactFragment = null;
    private FindFragment findFragment = null;
    private MeFragment meFragment = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mbtnWeChat = findViewById(R.id.wechat);
        mbtnContact = findViewById(R.id.contact);
        mbtnFind = findViewById(R.id.find);
        mbtnMe = findViewById(R.id.me);

        mbtnWeChat.setOnClickListener(this);
        mbtnContact.setOnClickListener(this);
        mbtnFind.setOnClickListener(this);
        mbtnMe.setOnClickListener(this);
        selectTab(0);
    }

    @Override
    public void onClick(View v){
        setdefault();
        switch (v.getId()){
            case R.id.wechat:
                mbtnWeChat.setImageResource(R.drawable.weixin_pressed);
                selectTab(0);
                break;
            case R.id.contact:
                mbtnContact.setImageResource(R.drawable.contact_list_pressed);
                selectTab(1);
                break;
            case R.id.find:
                mbtnFind.setImageResource(R.drawable.find_pressed);
                selectTab(2);
                break;
            case R.id.me:
                mbtnMe.setImageResource(R.drawable.profile_pressed);
                selectTab(3);
                break;
        }
    }

    public void selectTab(int index){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(weChatFragment != null){
            transaction.hide(weChatFragment);
        }
        if(contactFragment != null){
            transaction.hide(contactFragment);
        }
        if(findFragment != null){
            transaction.hide(findFragment);
        }
        if(meFragment != null){
            transaction.hide(meFragment);
        }
        switch (index){
            case 0:
                if(weChatFragment == null){
                    weChatFragment = new WeChatFragment();
                    transaction.add(R.id.f_content,weChatFragment);
                }else {
                    transaction.show(weChatFragment);
                }break;
            case 1:
                if(contactFragment == null){
                    contactFragment = new ContactFragment();
                    transaction.add(R.id.f_content,contactFragment);
                }else {
                    transaction.show(contactFragment);
                }break;
            case 2:
                if(findFragment == null){
                    findFragment = new FindFragment();
                    transaction.add(R.id.f_content,findFragment);
                }else {
                    transaction.show(findFragment);
                }break;
            case 3:
                if(meFragment == null){
                    meFragment = new MeFragment();
                    transaction.add(R.id.f_content,meFragment);
                }else {
                    transaction.show(meFragment);
                }break;

        }transaction.commit();
    }

    public void setdefault(){
        mbtnWeChat.setImageResource(R.drawable.weixin_normal);
        mbtnContact.setImageResource(R.drawable.contact_list_normal);
        mbtnFind.setImageResource(R.drawable.find_normal);
        mbtnMe.setImageResource(R.drawable.profile_normal);
    }


}
